package org.kunteng.yunac.modules.sys.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ShiYun on 2015/11/25 0025.
 */
public class FileAnalysis {

    /**
     * 导入路由为TXT格式文件时解析
     *
     * @param file
     * @return
     */
    public static List<String> getTXTContent(String file) {
        List<String> list = new ArrayList<>();
        File efile = new File(file);
        FileReader fr = null;
        try {
            fr = new FileReader(efile);
            BufferedReader br = new BufferedReader(fr);
            String str = null;
            while ((str = br.readLine()) != null) {
                System.out.println(str+"\t"+str.toUpperCase());
                list.add(str.toUpperCase());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 导入路由表为EXCEL文档时解析
     *
     * @param file
     * @return
     */
    public static List<String> getExcelContent(String file) {
        List<String> list = new ArrayList<>();
        File efile = new File(file);
        HSSFWorkbook workbook = null;
        try {
            workbook = new HSSFWorkbook(new FileInputStream(efile));
            //获得指定的表
            HSSFSheet sheet = workbook.getSheetAt(0);
            //获得数据总行数
            int rowCount = sheet.getLastRowNum();
            System.out.println("总行数:" + rowCount);
            //逐行读取数据
            for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
                //获得行对象
                HSSFRow row = sheet.getRow(rowIndex);
                if (row != null) {
//                  List<Object> rowData = new ArrayList<Object>();
                    //获得本行中单元格的个数
                    int columnCount = row.getLastCellNum();
                    //获得本行中各单元格中的数据
                    HSSFCell cell0 = row.getCell(0);
                    HSSFCell cell1 = row.getCell(1);
                    HSSFCell cell2 = row.getCell(2);
//                    System.out.println("第一格:" + cell0 + " 第二格:" + cell1 + " 第三格:" + cell2);
                    list.add(cell0.toString().toUpperCase());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("------------------------------");
        return list;
    }

}
